/*
  Copyright 2007 Anders Båtstrand

      Licensed under the Apache License, Version 2.0 (the "License");
      you may not use this file except in compliance with the License.
      You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      See the License for the specific language governing permissions and
      limitations under the License.

 */

package org.dyndns.anderlin.jmplayer.client.ui.binder;

import org.dyndns.anderlin.jmplayer.model.PieceCategory;
import org.dyndns.anderlin.jmplayer.service.PieceCategoryService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.binding.form.FormModel;
import org.springframework.richclient.form.binding.Binding;
import org.springframework.richclient.form.binding.support.AbstractBinder;
import org.springframework.richclient.form.binding.swing.ComboBoxBinding;
import org.springframework.util.Assert;

import javax.swing.*;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: anderlin
 * Date: May 31, 2007
 * Time: 12:45:56 PM
 * To change this template use File | Settings | File Templates.
 */
public class PieceCategoryBinder extends AbstractBinder implements InitializingBean {

    private PieceCategoryService pieceCategoryService;

    public PieceCategoryBinder() {
        super(PieceCategory.class);
    }


    public void setPieceCategoryService(PieceCategoryService pieceCategoryService) {
        this.pieceCategoryService = pieceCategoryService;
    }

    public void afterPropertiesSet() throws Exception {
        Assert.notNull(pieceCategoryService, "A pieceCategoryService must be provided.");
    }

    protected JComponent createControl(Map context) {
        return getComponentFactory().createComboBox();
    }

    protected Binding doBind(JComponent control, FormModel formModel, String formPropertyPath, Map context) {
        Assert.isTrue(control instanceof JComboBox, formPropertyPath);
        ComboBoxBinding binding = new ComboBoxBinding((JComboBox) control, formModel, formPropertyPath);
        binding.setSelectableItems(pieceCategoryService.getAllPieceCategories());
        return binding;
    }
}